package com.hxl.olympic.fragment;

import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hxl.okhttp.response.JsonResponseHandler;
import com.hxl.olympic.R;
import com.hxl.olympic.pojo.User;
import com.hxl.olympic.util.Constant;
import com.xuexiang.xui.widget.toast.XToast;

import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.OnClick;


public class RegisterInfoFragment extends BaseFragment implements View.OnClickListener {

    @BindView(R.id.et_username)
    EditText etUsername;
    @BindView(R.id.et_nickname)
    EditText etNickname;
    @BindView(R.id.et_email)
    EditText etEmail;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.img_display)
    ImageView imgDisplay;
    @BindView(R.id.et_affirm_pwd)
    EditText etAffirmPwd;
    @BindView(R.id.btn_register)
    Button btnRegister;
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    private boolean display = false;
    private String phone;

    @Override
    public void initialize() {

    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Override
    public void initViews() {

    }

    @Override
    public void initData() {
        tvTitle.setText(getResources().getString(R.string.register));
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_register_info;
    }

    @Override
    @OnClick({R.id.img_display, R.id.btn_register, R.id.img_back})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_back:
                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        fragmentManager.popBackStack();
                    }
                }, 500);
                break;
            case R.id.img_display:
                if (display) {
                    etPwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    imgDisplay.setBackgroundResource(R.mipmap.ic_bly);
                    display = false;
                } else {
                    imgDisplay.setBackgroundResource(R.mipmap.ic_bmq);
                    etPwd.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    display = true;
                }
                break;
            case R.id.btn_register:
                register();
                break;
        }
    }

    private void register() {
        String username = etUsername.getText().toString().trim();
        String nickname = etNickname.getText().toString().trim();
        String email = etEmail.getText().toString().trim();
        String pwd = etPwd.getText().toString().trim();
        String affirm_pwd = etAffirmPwd.getText().toString().trim();
        if (username.equals("") || pwd.equals("") || nickname.equals("")
                || email.equals("") || affirm_pwd.equals("")) {
            XToast.info(activity, "您的输入不完整!", Toast.LENGTH_SHORT).show();
            return;
        } else if (username.length() < 6) {
            XToast.info(activity, "用户名过短!", Toast.LENGTH_SHORT).show();
            return;
        } else if (etPwd.length() < 8) {
            XToast.info(activity, "密码设置过短!", Toast.LENGTH_SHORT).show();
            return;
        } else if (!pwd.equals(affirm_pwd)) {
            XToast.info(activity, "两次输入的密码不一致!", Toast.LENGTH_SHORT).show();
            return;
        }
        User user = new User(username, pwd, nickname, null, null, phone, email, 100, 1, null, null);
        String json = new Gson().toJson(user);
        System.out.println(json);
        dialog.show();
        app.getOkHttpUtils().post(Constant.BASE_URL + Constant.register, json, new JsonResponseHandler() {
            @Override
            public void onSuccess(int statusCode, JSONObject response) {
                try {
                    if (response.getString("").equals("OK")) {
                        dialog.cancel();
                        app.setUserInfo(new Gson().fromJson(response.toString(), (Type) User.class));
                        editor.putString("userInfo", response.toString());
                        editor.commit();
                        XToast.success(activity, "注册成功!", Toast.LENGTH_SHORT).show();
                        activity.finish();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int statusCode, String error_msg) {
                if (statusCode == 200) {
                    XToast.warning(activity, "注册失败!", Toast.LENGTH_SHORT).show();
                } else if (statusCode == 0) {
                    XToast.warning(activity, "用户名或邮箱已被注册!", Toast.LENGTH_SHORT).show();
                } else {
                    XToast.error(activity, "网络请求失败!", Toast.LENGTH_SHORT).show();
                }
                dialog.cancel();
            }
        });
    }

}
